/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao.imp;

import dao.VentaDao;
import hibernate.HibernateUtil;
import java.util.ArrayList;
import modelo.dominio.Articulo;
import modelo.dominio.Venta;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

/**
 *
 * @author Pablo
 */
public class VentaDaoImp implements VentaDao {

    @Override
    public void insert(Venta venta) {
         Session session = HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();
        session.save(venta);
        session.getTransaction().commit();
        session.close();   
    }

    @Override
    public void delete(Venta venta) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();
        session.delete(venta);
        session.getTransaction().commit();
        session.close();   
    }

    @Override
    public void update(Venta venta) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();
        session.update(venta);
        session.getTransaction().commit();
        session.close();   
    }

    @Override
    public Venta getVenta(int id) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();
        Criteria criteria = session.createCriteria(Venta.class);
        criteria.add(Restrictions.eq("id", id));
        Venta venta = (Venta) criteria.list().get(0);
        session.close();
        return venta;
    }

    @Override
    public ArrayList<Venta> getAll() {
         Session session = HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();
        Criteria criteria = session.createCriteria(Venta.class);
        criteria.addOrder(Order.asc("id"));
        ArrayList<Venta> list = (ArrayList<Venta>) criteria.list();
        session.close();
        return list;
    }
    
    
}
